package games.ninJumpDx.ui.actions
{
	import actions.PropsAction;
	import actions.PropsActionGroup;
	
	import core.Global;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.getTimer;
	
	import games.ninJumpDx.data.UIConfig;
	import utils.ActionFactory;
	import games.ninJumpDx.ui.factorys.MunitFactory;
	
	import utils.Cartoon;
	
	public class Ninja extends Cartoon
	{
		public static const NINJA_CLIMBING:String = "climbing";
		public static const NINJA_FALLING:String = "falling";
		public static const NINJA_JUMPING:String = "jumping";
		public static const NINJA_TROLL:String 	 = "troll_bonus";
		public static const NINJA_GARGOYLE:String = "gargoyle_bonus"
		
		private var m_state:String;
		private var m_actionCreator:ActionFactory;
		
		private var m_direction:String;
		
		public function Ninja()
		{
			super();
			m_actionCreator = new MunitFactory();
			x = UIConfig.getInfoByName("ninja").@x;
			y = UIConfig.getInfoByName("ninja").@y;
			state = Ninja.NINJA_CLIMBING;
			Global.stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		override public function destroy(e:*=null):void
		{
			super.destroy();
			Global.stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		
		public function set state(n:String):void
		{
			m_state = n;
			changeAction();
			changeDirection();
		}
		
		public function get state():String
		{
			return m_state;
		}
		
		private function clear():void
		{
			while(numChildren > 0)
			{
				var mc:MoveUnit = MoveUnit(removeChildAt(0));
				mc.destroy();
			}
		}
		
		private function changeAction():void
		{
			clear();
			var ani:Sprite = m_actionCreator.makeAction(m_state);
			addChild(ani);
			switch(m_state)
			{
				case Ninja.NINJA_CLIMBING:
					break ;
				case Ninja.NINJA_FALLING:
					break ;
				case Ninja.NINJA_JUMPING:
					move();
					ani.scaleX = -1.5;
					ani.scaleY = 1.5;
					break ;
				case Ninja.NINJA_TROLL:
					break;
				case Ninja.NINJA_GARGOYLE:
					break;
			}
		}
		
		private function changeDirection():void
		{
			m_direction = x <= UIConfig.getInfoByName("ninja").@left ?"left":"right";
			if(m_direction == "left")
			{
				scaleX = 1;
			}
			else
			{
				scaleX = -1;				
			}
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			if(Global.pause)return;
			if(state == Ninja.NINJA_CLIMBING)
			{
				state = Ninja.NINJA_JUMPING;
			}
			
		}
		
		private function move():void
		{
			var pa:PropsAction = new PropsAction(this);
			if(m_direction == "left")
			{
				pa.props = [{prop:"x",init:x,end:(UIConfig.getInfoByName("ninja").@right)}];
		
			}else
			{
				pa.props = [{prop:"x",init:x,end:(UIConfig.getInfoByName("ninja").@left)}];
			
			}
			pa.speed = .05;
			pa.addEventListener(Event.COMPLETE,onCompletePa);
			Global.addAction(pa);
			
			var pys:PropsActionGroup = new PropsActionGroup();
			pys.type = PropsActionGroup.SERIAL;
			var py1:PropsAction = new PropsAction(this);
			var midPointY:Number = Number(UIConfig.getInfoByName("ninja").@top)
			py1.props = [{prop:"y",init:y,end:midPointY}];
			py1.speed = .1;
			var py2:PropsAction = new PropsAction(this);
			py2.props = [{prop:"y",init:midPointY,end:(UIConfig.getInfoByName("ninja").@y)}];
			py2.speed = .1;
			pys.list = new Vector.<PropsAction>();
			pys.list.push(py1,py2);
			Global.addAction(pys);
		}
		
		private function onCompletePa(e:Event):void
		{
			e.target.removeEventListener(Event.COMPLETE,onCompletePa);
			state = Ninja.NINJA_CLIMBING;
		}
	}
}